package com.jesse.eclgy.plug.util;


import java.util.List;
import com.jesse.eclgy.plug.workflow.entity.CustomMap;
import weaver.soa.workflow.request.Cell;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.DetailTableInfo;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.Row;

/**
 * Description:
 * Author: jesse
 * Date:2023/7/11 15:11
 */
public class RequestUtils {

    public static MainTableInfo toMainTable(CustomMap fields)
    {
        MainTableInfo mti = new MainTableInfo();
        for (String name : fields.keySet())
        {
            mti.addProperty(toMainField(name, fields.getString(name)));
        }
        return mti;
    }

    public static DetailTableInfo toDetailTableInfo(List<List<CustomMap>> tables)
    {
        DetailTableInfo dti = new DetailTableInfo();
        for (int i = 0; i < tables.size(); i++)
        {
            dti.addDetailTable(toDetailTable(i + 1, tables.get(i)));
        }
        return dti;
    }

    public static DetailTable toDetailTable(int id, List<CustomMap> rows)
    {
        DetailTable dt = new DetailTable();
        dt.setId(id + "");
        for (CustomMap rowMap : rows)
        {
            dt.addRow(toRow(rowMap));
        }
        return dt;
    }

    public static Property toMainField(String name, String value)
    {
        if (name == null || name.isEmpty())
        {
            throw new RuntimeException("字段名不能为空！");
        }
        ;
        if (value == null)
        {
            value = "";
        }
        ;

        Property field = new Property();
        field.setName(name);
        field.setValue(value);
        return field;
    }

    public static Row toRow(CustomMap rowMap)
    {
        Row row = new Row();
        for (String name : rowMap.keySet())
        {
            row.addCell(toCell(name, rowMap.getString(name)));
        }
        return row;
    }

    public static Cell toCell(String name, String value)
    {
        if (name == null || name.isEmpty())
        {
            throw new RuntimeException("字段名不能为空！");
        }
        if (value == null)
        {
            value = "";
        }

        Cell field = new Cell();
        field.setName(name);
        field.setValue(value);
        return field;
    }

}